/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia.JPAControllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Logica.Core.Depositos.Lote;
import Logica.Core.Producto.LeñaSeca;
import Persistencia.JPAControllers.exceptions.NonexistentEntityException;
import Persistencia.JPAControllers.exceptions.PreexistingEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Luciano
 */
public class LeñaSecaJpaController implements Serializable {

    public LeñaSecaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public LeñaSecaJpaController() {
        emf = Persistence.createEntityManagerFactory("SecaderoPU");
    }

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(LeñaSeca leñaSeca) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Lote miLote = leñaSeca.getMiLote();
            if (miLote != null) {
                miLote = em.getReference(miLote.getClass(), miLote.getNumero());
                leñaSeca.setMiLote(miLote);
            }
            em.persist(leñaSeca);
            if (miLote != null) {
                Logica.Core.Producto.Producto oldMiProductoOfMiLote = miLote.getMiProducto();
                if (oldMiProductoOfMiLote != null) {
                    oldMiProductoOfMiLote.setMiLote(null);
                    oldMiProductoOfMiLote = em.merge(oldMiProductoOfMiLote);
                }
                miLote.setMiProducto(leñaSeca);
                miLote = em.merge(miLote);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findLeñaSeca(leñaSeca.getId()) != null) {
                throw new PreexistingEntityException("Le\u00f1aSeca " + leñaSeca + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(LeñaSeca leñaSeca) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            LeñaSeca persistentLeñaSeca = em.find(LeñaSeca.class, leñaSeca.getId());
            Lote miLoteOld = persistentLeñaSeca.getMiLote();
            Lote miLoteNew = leñaSeca.getMiLote();
            if (miLoteNew != null) {
                miLoteNew = em.getReference(miLoteNew.getClass(), miLoteNew.getNumero());
                leñaSeca.setMiLote(miLoteNew);
            }
            leñaSeca = em.merge(leñaSeca);
            if (miLoteOld != null && !miLoteOld.equals(miLoteNew)) {
                miLoteOld.setMiProducto(null);
                miLoteOld = em.merge(miLoteOld);
            }
            if (miLoteNew != null && !miLoteNew.equals(miLoteOld)) {
                Logica.Core.Producto.Producto oldMiProductoOfMiLote = miLoteNew.getMiProducto();
                if (oldMiProductoOfMiLote != null) {
                    oldMiProductoOfMiLote.setMiLote(null);
                    oldMiProductoOfMiLote = em.merge(oldMiProductoOfMiLote);
                }
                miLoteNew.setMiProducto(leñaSeca);
                miLoteNew = em.merge(miLoteNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                int id = leñaSeca.getId();
                if (findLeñaSeca(id) == null) {
                    throw new NonexistentEntityException("The le\u00f1aSeca with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(int id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            LeñaSeca leñaSeca;
            try {
                leñaSeca = em.getReference(LeñaSeca.class, id);
                leñaSeca.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The le\u00f1aSeca with id " + id + " no longer exists.", enfe);
            }
            Lote miLote = leñaSeca.getMiLote();
            if (miLote != null) {
                miLote.setMiProducto(null);
                miLote = em.merge(miLote);
            }
            em.remove(leñaSeca);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<LeñaSeca> findLeñaSecaEntities() {
        return findLeñaSecaEntities(true, -1, -1);
    }

    public List<LeñaSeca> findLeñaSecaEntities(int maxResults, int firstResult) {
        return findLeñaSecaEntities(false, maxResults, firstResult);
    }

    private List<LeñaSeca> findLeñaSecaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Le\u00f1aSeca as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public LeñaSeca findLeñaSeca(int id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(LeñaSeca.class, id);
        } finally {
            em.close();
        }
    }

    public int getLeñaSecaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Le\u00f1aSeca as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
